(function () {

    "use strict";

    var api = {
        getDetailUrl: "/base/detail"
    };

    sm.controller('wx-base-index-controller', ['$scope', '$http', '$sce', function ($scope, $http, $sce){

        var search = window.location.search;
        var baseId = search.split('=')[1];

        $scope.detail = {};
        $scope.trustHtml = '';
        $scope.related = [];
        $scope.goods = [];
        $scope.isLive = false;
        $http.get(api.getDetailUrl, {
            params: {
                baseId: baseId
            }
        }).success(function (res) {
            if (res.state) {
                $scope.detail = res.data;
                $scope.trustHtml = $sce.trustAsHtml($scope.detail.content);
                $scope.goods = res.data.goods.data;
                $scope.related = res.data.related.data;

                if ($scope.liveUrl && $scope.liveUrl != '') {
                    $scope.isLive = true;
                }

                setTimeout(function () {
                    calcImageWidth();
                });
            }
        });

        $scope.showLive = function () {

            if (!$scope.isLive) {
                return false;
            }

            location.href = $scope.detail.liveUrl;
        };

        function calcImageWidth() {
            var w = $('.gu-base-goods-image').width();
            var relatedWidth = $('.gu-base-related-thumb').width();
            $('.gu-base-goods-image img').height(w);
            $('.gu-base-related-thumb img').height(relatedWidth);
        }


    }]);
})();

